package com.open.capacity.task.utils;

import com.open.capacity.common.model.SysTask;
import com.open.capacity.task.dto.TaskReplyCommentDto;
import com.open.capacity.task.model.TaskReplyAndComment;
import com.vdurmont.emoji.EmojiParser;
import org.apache.commons.lang.StringUtils;
import org.commonmark.Extension;
import org.commonmark.ext.gfm.tables.TablesExtension;
import org.commonmark.node.Node;
import org.commonmark.renderer.html.HtmlRenderer;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * @Author moonholder
 * @Description 公共函数
 * @Date 19:34 2021/3/9
 */
public class Commons {
    /**
     * 显示文章内容，转换markdown为HTML
     *
     * @param markdown markdown文本
     * @return HTML文本
     */
//    public static String mdToHtml(String markdown) {
//        if (StringUtils.isNotBlank(markdown)) {
//            markdown = markdown.replace("<!--more-->", "\r\n");
//            markdown = markdown.replace("<!-- more -->", "\r\n");
//            List<Extension> extensions = Collections.singletonList(TablesExtension.create());
//            org.commonmark.parser.Parser parser = org.commonmark.parser.Parser.builder().extensions(extensions).build();
//            Node document = parser.parse(markdown);
//            HtmlRenderer renderer = HtmlRenderer.builder().extensions(extensions).build();
//            String content = renderer.render(document);
//            content = EmojiParser.parseToUnicode(content);
//            return content;
//        }
//        return "";
//    }


    /**
     * 填充子级评论
     *
     * @param commentList 评论集合
     * @return
     */
    public static List<TaskReplyCommentDto> fillCommentChildren(List<TaskReplyCommentDto> commentList) {
        List<TaskReplyCommentDto> result = new ArrayList<>();
        for (TaskReplyCommentDto taskReplyCommentDto : commentList) {
            if (taskReplyCommentDto.getPtoid() == null || taskReplyCommentDto.getPtoid() == 0L) {
                result.add(findChildren(taskReplyCommentDto,commentList));
            }
        }
        return result;
    }

    private static TaskReplyCommentDto findChildren(TaskReplyCommentDto item, List<TaskReplyCommentDto> protoList) {
        return item.setChildren(getCommentChild(item, protoList));
    }

    private static List<TaskReplyCommentDto> getCommentChild(TaskReplyCommentDto parentItem,List<TaskReplyCommentDto> protoList) {
        List<TaskReplyCommentDto> childList = new ArrayList<>();
        for (TaskReplyCommentDto item : protoList) {
            Long parentId = item.getPtoid();
            if (parentId != null && parentId.equals(parentItem.getPtoid())) {
                childList.add(item);
            }
        }
        return childList;
    }

}
